package com.openbravo.pos.config;

import com.openbravo.basic.BasicException;
import com.openbravo.data.gui.JMessageDialog;
import com.openbravo.data.gui.MessageInf;
import com.openbravo.pos.forms.AppConfig;
import com.openbravo.pos.forms.AppLocal;
import com.openbravo.pos.forms.AppProperties;
import com.openbravo.pos.forms.AppView;
import com.openbravo.pos.forms.JPanelView;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.swing.BoxLayout;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.GroupLayout.ParallelGroup;
import javax.swing.GroupLayout.SequentialGroup;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.LayoutStyle;
import javax.swing.LayoutStyle.ComponentPlacement;

public class JPanelConfiguration extends JPanel
  implements JPanelView
{
  private List<PanelConfig> m_panelconfig;
  private AppConfig config;
  private JScrollPane jScrollPane1;
  private JButton jbtnCancel;
  private JButton jbtnRestore;
  private JButton jbtnSave;
  private JPanel m_jConfigOptions;

  public JPanelConfiguration(AppView paramAppView)
  {
    this(paramAppView.getProperties());
  }

  public JPanelConfiguration(AppProperties paramAppProperties)
  {
    this.config = new AppConfig(paramAppProperties.getConfigFile());
    initComponents();
    this.m_panelconfig = new ArrayList();
    this.m_panelconfig.add(new JPanelConfigDatabase());
    this.m_panelconfig.add(new JPanelConfigGeneral());
    this.m_panelconfig.add(new JPanelConfigLocale());
    this.m_panelconfig.add(new JPanelConfigPayment());
    Iterator localIterator = this.m_panelconfig.iterator();
    while (localIterator.hasNext())
    {
      PanelConfig localPanelConfig = (PanelConfig)localIterator.next();
      this.m_jConfigOptions.add(localPanelConfig.getConfigComponent());
    }
  }

  private void restoreProperties()
  {
    if (this.config.delete())
      loadProperties();
    else
      JMessageDialog.showMessage(this, new MessageInf(-33554432, AppLocal.getIntString("message.cannotdeleteconfig")));
  }

  private void loadProperties()
  {
    this.config.load();
    Iterator localIterator = this.m_panelconfig.iterator();
    while (localIterator.hasNext())
    {
      PanelConfig localPanelConfig = (PanelConfig)localIterator.next();
      localPanelConfig.loadProperties(this.config);
    }
  }

  private void saveProperties()
  {
    Iterator localIterator = this.m_panelconfig.iterator();
    while (localIterator.hasNext())
    {
      PanelConfig localPanelConfig = (PanelConfig)localIterator.next();
      localPanelConfig.saveProperties(this.config);
    }
    try
    {
      this.config.save();
      JOptionPane.showMessageDialog(this, AppLocal.getIntString("message.restartchanges"), AppLocal.getIntString("message.title"), 1);
    }
    catch (IOException localIOException)
    {
      JMessageDialog.showMessage(this, new MessageInf(-33554432, AppLocal.getIntString("message.cannotsaveconfig"), localIOException));
    }
  }

  public JComponent getComponent()
  {
    return this;
  }

  public String getTitle()
  {
    return AppLocal.getIntString("Menu.Configuration");
  }

  public void activate()
    throws BasicException
  {
    loadProperties();
  }

  public boolean deactivate()
  {
    int i = 0;
    Iterator localIterator = this.m_panelconfig.iterator();
    while (localIterator.hasNext())
    {
      PanelConfig localPanelConfig = (PanelConfig)localIterator.next();
      if (localPanelConfig.hasChanged())
        i = 1;
    }
    if (i != 0)
    {
      int j = JOptionPane.showConfirmDialog(this, AppLocal.getIntString("message.wannasave"), AppLocal.getIntString("title.editor"), 1, 3);
      if (j == 0)
      {
        saveProperties();
        return true;
      }
      return j == 1;
    }
    return true;
  }

  private void initComponents()
  {
    this.jScrollPane1 = new JScrollPane();
    this.m_jConfigOptions = new JPanel();
    this.jbtnCancel = new JButton();
    this.jbtnRestore = new JButton();
    this.jbtnSave = new JButton();
    this.m_jConfigOptions.setLayout(new BoxLayout(this.m_jConfigOptions, 1));
    this.jScrollPane1.setViewportView(this.m_jConfigOptions);
    this.jbtnCancel.setText(AppLocal.getIntString("Button.Restore"));
    this.jbtnCancel.addActionListener(new ActionListener()
    {
      public void actionPerformed(ActionEvent paramActionEvent)
      {
        JPanelConfiguration.this.jbtnCancelActionPerformed(paramActionEvent);
      }
    });
    this.jbtnRestore.setText(AppLocal.getIntString("Button.Factory"));
    this.jbtnRestore.addActionListener(new ActionListener()
    {
      public void actionPerformed(ActionEvent paramActionEvent)
      {
        JPanelConfiguration.this.jbtnRestoreActionPerformed(paramActionEvent);
      }
    });
    this.jbtnSave.setText(AppLocal.getIntString("Button.Save"));
    this.jbtnSave.addActionListener(new ActionListener()
    {
      public void actionPerformed(ActionEvent paramActionEvent)
      {
        JPanelConfiguration.this.jbtnSaveActionPerformed(paramActionEvent);
      }
    });
    GroupLayout localGroupLayout = new GroupLayout(this);
    setLayout(localGroupLayout);
    localGroupLayout.setHorizontalGroup(localGroupLayout.createParallelGroup(GroupLayout.Alignment.LEADING).addGroup(GroupLayout.Alignment.TRAILING, localGroupLayout.createSequentialGroup().addContainerGap().addGroup(localGroupLayout.createParallelGroup(GroupLayout.Alignment.TRAILING).addComponent(this.jScrollPane1, GroupLayout.Alignment.LEADING, -1, 595, 32767).addGroup(localGroupLayout.createSequentialGroup().addComponent(this.jbtnSave).addPreferredGap(LayoutStyle.ComponentPlacement.RELATED).addComponent(this.jbtnRestore).addPreferredGap(LayoutStyle.ComponentPlacement.RELATED).addComponent(this.jbtnCancel))).addContainerGap()));
    localGroupLayout.setVerticalGroup(localGroupLayout.createParallelGroup(GroupLayout.Alignment.LEADING).addGroup(GroupLayout.Alignment.TRAILING, localGroupLayout.createSequentialGroup().addContainerGap().addComponent(this.jScrollPane1, -1, 278, 32767).addPreferredGap(LayoutStyle.ComponentPlacement.RELATED).addGroup(localGroupLayout.createParallelGroup(GroupLayout.Alignment.BASELINE).addComponent(this.jbtnCancel).addComponent(this.jbtnRestore).addComponent(this.jbtnSave)).addContainerGap()));
  }

  private void jbtnCancelActionPerformed(ActionEvent paramActionEvent)
  {
    if (JOptionPane.showConfirmDialog(this, AppLocal.getIntString("message.configrestore"), AppLocal.getIntString("message.title"), 0, 3) == 0)
      loadProperties();
  }

  private void jbtnRestoreActionPerformed(ActionEvent paramActionEvent)
  {
    if (JOptionPane.showConfirmDialog(this, AppLocal.getIntString("message.configfactory"), AppLocal.getIntString("message.title"), 0, 3) == 0)
      restoreProperties();
  }

  private void jbtnSaveActionPerformed(ActionEvent paramActionEvent)
  {
    saveProperties();
  }
}

/* Location:           D:\PowerFulPOS-2.30.2\openbravopos.jar
 * Qualified Name:     com.openbravo.pos.config.JPanelConfiguration
 * JD-Core Version:    0.6.0
 */